<?php

namespace App\Http\Controllers;

use App\Article;
use App\Column;
use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Http\Request;

class ArticleController extends Controller {
	/**
	 * Display a listing of the resource.
	 *
	 * @return \Illuminate\Http\Response
	 */
	public function index(Request $request) {
		$flag = false;
		$clm = [];
		//筛选条件 : 文章标题, 作者, 栏目, 文章状态, 排序, 每页条数
		$columns = Column::get(); //获取栏目清单
		foreach ($columns as $k => $v) {
			$clm[$v->id] = $v->title;
		}
		// dd($clm);
		// $topics = Topic::get();//获取话题清单
		$articles = Article::where(function ($query) use ($request) {
			if (!empty($request->title)) {
				$query->where('title', 'like', '%' . $request->title . '%');
			}
			if (!empty($request->status)) {
				$query->where('status', $request->status);
			}
			if (!empty($request->column)) {
				$query->where('column_id', $request->column);
			}
			if (!empty($request->author)) {
				$query->where('user_id', $request->author);
			}
			$query->where('status', '<', 3);
		})->orderBy($request->input('sort', 'id'), 'desc')->paginate($request->input('num', 10));
		return view('admin.article.index', [
			'title' => '文章列表',
			'articles' => $articles,
			'request' => $request,
			'columns' => $clm,
			'flag' => $flag,
		]);
	}
	//获取回收站中已删除状态的文章
	public function showDelArt(Request $request) {
		$flag = true;
		$clm = [];
		//筛选条件 : 文章标题, 作者, 话题, 栏目, 文章状态, 排序, 每页条数
		$columns = Column::get(); //获取栏目清单
		foreach ($columns as $k => $v) {
			$clm[$v->id] = $v->title;
		}
		// $topics = Topic::get();//获取话题清单
		$articles = Article::where(function ($query) use ($request) {
			if (!empty($request->title)) {
				$query->where('title', 'like', '%' . $request->title . '%');
			}
			if (!empty($request->status)) {
				$query->where('status', $request->status);
			}
			if (!empty($request->column)) {
				$query->where('column_id', $request->column);
			}
			if (!empty($request->author)) {
				$query->where('user_id', $request->author);
			}
			$query->where('status', 3);
		})->orderBy($request->input('sort', 'id'), 'desc')->paginate($request->input('num', 10));
		return view('admin.article.index', [
			'title' => '文章回收站',
			'articles' => $articles,
			'request' => $request,
			'columns' => $clm,
			'flag' => $flag,
		]);
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return \Illuminate\Http\Response
	 */
	public function edit($id) {
		$article = Article::findOrFail($id);
		$user = User::findOrFail($article->user_id)->username;
		// $topic = Topic::findOrFail($article->topic_id)->title; //获取话题
		$topic = '谈天说地';
		$column = Column::find($article->column_id)->title; //获取栏目

		return view('admin.article.show', [
			'title' => '文章详情',
			'art' => $article,
			'username' => $user,
			'topic' => $topic,
			'column' => $column,
		]);
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  \Illuminate\Http\Request  $request
	 * @param  int  $id
	 * @return \Illuminate\Http\Response
	 */
	public function update(Request $request, $id) {
		$art = Article::find($id);
		$art->status = 2;
		if ($art->save()) {
			return redirect('/admin/article');
		} else {
			return back();
		}
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return \Illuminate\Http\Response
	 */
	public function destroy($id) {
		$art = Article::find($id);
		$art->status = 3;
		if ($art->save()) {
			return redirect('/admin/article');
		} else {
			return back();
		}
	}
}
